// -----------------------------------------------
// Author:     code34 nicolas_boiteux@yahoo.fr
// Edited by:  Neumatic
// Warcontext: Create propagander in towns
// -----------------------------------------------
if (!isServer) exitWith {};

private [
	"_needpropagander",
	"_position",
	"_conversion",
	"_men",
	"_positions",
	"_missioncomplete",
	"_civilrole",
	"_group",
	"_unit"
];

_unit = _this select 0;

_needpropagander = true;

{
	if (_x distance _unit < 500) then {
		_needpropagander = false;
	};
	if (isNull _x) then {wcpropagander = wcpropagander - [_x]};
} foreach wcpropagander;

if (!_needpropagander) exitWith {};

_men = nearestObjects [_unit, ["Man"], 400];
if (count _men == 0) exitWith {};

wcpropagander = wcpropagander + [_unit];

diag_log format ["WARCONTEXT: BUILD 1 PROPAGANDER - fame: %1", wcfame];

_position = (position _unit) findEmptyPosition [8, 100];
if (_position select 0 == 0) then {
	diag_log "WARCONTEXT: CANNOT FIND EMPTY POSITION FOR PROPAGAND CIVILIAN";
} else {
	_unit setPos _position;
};

_unit setVariable ["wcprotected", true];
_unit stop true;
_unit disableAI "MOVE";

_conversion = 0;
_missioncomplete = false;

while {((alive _unit) and (_conversion < 1000) and (!_missioncomplete))} do {
	_men = nearestObjects [_unit, ["Man"], 400];
	_men = _men - [_unit];
	if (count _men > 1) then {
		_positions = [position _unit, 5, 360, getDir _unit, 5] call WC_fnc_createcircleposition;
		_unit stop true;
		{
			if ((side _x == civilian) and ((_x getVariable "civilrole" == "civil") or (_x getVariable "civilrole" == "converting") or (_x getVariable "civilrole" == "converted"))) then {
				if(_x distance _unit > 7) then {
					_position = _positions call BIS_fnc_selectRandom;
					_x stop false;
					_x doMove _position;
					_x setVariable ["destination", _position, false];
					_x setVariable ["civilrole", "converting", true];
				} else {
					_x doWatch _unit;
					_x setUnitPos "Up";
					_x setVariable ["civilrole", "converted", true];
					_x stop true;
					_conversion = _conversion + 1;
				};
				_unit setVariable ["convertingrate", _conversion, false];
			} else {
				_men = _men - [_x];
			};
			if (isPlayer _x) then {
				_men = _men - [_x];
			};
		} foreach _men;
	};
	if ((getMarkerPos "jail") distance _unit < 50) then {
		wcmessageW = ["Propagand", "A prisonner is in jail"];
		["wcmessageW", "client"] call WC_fnc_publicvariable;
		_unit setPos getMarkerPos "jail";
		[_unit] joinSilent group prisoner;
		_unit allowDammage false;
		_unit setUnitPos "Up"; 
		_unit playMoveNow "AmovPercMstpSnonWnonDnon_AmovPercMstpSsurWnonDnon";
		_unit stop true;
		_unit disableAI "MOVE";
		_unit disableAI "ANIM";
		wcfame = wcfame + 0.1;
		_missioncomplete = true;
		_conversion = 0;
	};
	sleep 0.5;
};

// If converted success
// Create a resistance group with weapons
if (!(_missioncomplete) and (alive _unit)) then {
	_unit stop false;
	_unit setVariable ["civilrole", "converted", true];

	_group = createGroup east;
	_men = nearestObjects [_unit, ["Man"], 30];
	{
		if ((_x getVariable "civilrole" == "converted") and !(isPlayer _x)) then {
			if !((typeOf _x) in wccivilwithoutweapons) then {
				_x stop false;
				removeAllWeapons _x;
				_x addEventHandler ["Fired", {(_this select 0) setVehicleAmmo 1}];
				_x removeAllEventHandlers "Killed";
				_x removeAllEventHandlers "FiredNear";
				_x addWeapon "AKS_74";
				_x addMagazine "30Rnd_545x39_AK";
			} else {
				_civilrole = ["bomberman","propagander","altercation","saboter","builder","healer"] call BIS_fnc_selectRandom;
				_x setVariable ["civilrole", _civilrole, true];
				_men = _men - [_x];
			};
		} else {
			_men = _men - [_x];
		};
	} foreach _men;

	_men joinSilent _group;
	wcgarbage = [_group] spawn WC_fnc_grouphandler;
	wcgarbage = [_group, (position (leader _group)), 300] spawn WC_fnc_patrol;
};